package com.hlc.helloWord.user.controller;

import com.hlc.helloWord.common.exception.MyException;
import com.hlc.helloWord.common.redis.RedisUtil;
import com.hlc.helloWord.common.result.ResultEnum;
import com.hlc.helloWord.common.result.ResultInfo;
import com.hlc.helloWord.user.entity.User;
import com.hlc.helloWord.user.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by helinchong on 2017/12/20.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    private Logger logger = Logger.getLogger(UserController.class);

    @ResponseBody
    @RequestMapping("/getUserInfoById")
    public ResultInfo getUserInfoById(@RequestBody User user){
        ResultInfo resultInfo = new ResultInfo();
        try {
            User user1 = userService.getUserInfoById(user);
            if(user1==null){
                resultInfo.setCode(ResultEnum.SUCCESS.getCode()).setMsg("暂无查询到数据!");
            }else{
                resultInfo.setCode(ResultEnum.SUCCESS.getCode()).setMsg(ResultEnum.SUCCESS.getValue()).setData(user1);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            resultInfo.setCode(ResultEnum.UNKNOWN_EXCETION.getCode()).setMsg(ResultEnum.UNKNOWN_EXCETION.getValue());
        }
        return resultInfo;
    }

    @ResponseBody
    @RequestMapping("/signIn")
    public ResultInfo signIn(@RequestBody User user){
        try {
            User loginUser = userService.signIn(user);
            return new ResultInfo().setCode(ResultEnum.SUCCESS.getCode()).setMsg("登录成功!").setData(loginUser);
        } catch (MyException e){
            logger.error(e.getMessage(),e);
            return new ResultInfo().setCode(ResultEnum.ERROR.getCode()).setMsg(e.getMessage());

        } catch (Exception e){
            logger.error(e.getMessage(),e);
            return new ResultInfo().setCode(ResultEnum.ERROR.getCode()).setMsg("未知异常!");
        }
    }

}
